package com.sgb.kjwl.utils;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;

import com.swgk.core.common.base.BaseApplication;
import com.swgk.core.util.PermissionUtils;
import com.swgk.core.util.SharedPreferenceUtil;

/**
 * @author: 杨畔
 * @date: 4/27/21 11:46 AM
 * @Description: 位置信息工具类
 */
public class LocationUtil {

    /**
     * 手机是否开启位置服务，如果没有开启那么所有app将不能使用定位功能
     */
    public static boolean isLocServiceEnable(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        return gps || network;
    }

    /**
     * 跳转设置 打开GPS
     */
    public static void openGPS(Context context) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        context.startActivity(intent);
    }

    /**
     * 判断是否开启定位权限和定位信息开关
     */
    public static boolean isOpenLocation(Context context) {
        if (context == null) context = BaseApplication.getApplication();
        return PermissionUtils.checkPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) && isLocServiceEnable(context);
    }

    /**
     * 获取当前纬度
     */
    public static double getCurrentLatitude() {
        return NumberUtil.convertToDouble(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "0"));
    }

    /**
     * 获取当前纬度
     */
    public static double getCurrentLongitude() {
        return NumberUtil.convertToDouble(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "0"));
    }

    public static boolean currentLocationIsValid() {
        if (getCurrentLongitude() == 0d && getCurrentLatitude() == 0d) {
            return false;
        }
        return true;
    }
}
